#include "BinTree.h"


void Initialize(SqList &L, BinTree &BST) {
    // 初始化，从文件读取
    FILE *fp;
    fp = fopen("text.txt", "at+");
    fscanf(fp, "%d", &L.length);
    for (int i = 1; i <= L.length; i++) {
        fscanf(fp, "%d %s %s %lf %d",
               &L.elem[i].ISBN, &L.elem[i].name,
               &L.elem[i].author, &L.elem[i].price,
               &L.elem[i].total);
        BST = Insert(BST, L.elem[i]);
    }
    QSort(L, 1, L.length); // 读取完文件进行一次快排
    fclose(fp);
}

void ListPrint(SqList L) {
    // 使用数组遍历输出文件
    FILE *fp;
    fp = fopen("demo.txt", "w+");
    fprintf(fp, "%d\n", L.length);
    for (int i = 1; i <= L.length; ++i) {
        fprintf(fp, "%d %s %s %.2lf %d\n", L.elem[i].ISBN, L.elem[i].name,
                L.elem[i].author, L.elem[i].price, L.elem[i].total);
    }
    fclose(fp);
}

void TreePrint(BinTree T) {
    // 使用中序遍历输出到控制台
    if (T) {
        TreePrint(T->Left);
        printf("%d %s %s %.2lf %d\n", T->Data.ISBN, T->Data.name, T->Data.author,
               T->Data.price, T->Data.total);
        TreePrint(T->Right);
    }
}

void AddBookNumber(SqList L, BinTree T) {
    int s, i, n;
    BinTree p;
    printf("输入需要增加数量的书号：");
    scanf("%d", &s);
    i = BinarySearch(L, s);
    if (i) {
        printf("当前数量%d，请输入删除的数量：", L.elem[i].total);
        scanf("%d", &n);
        L.elem[i].total += n;
        p = Find(T, L.elem[i]);
        p->Data.total += n;
        printf("增加数量成功！\n");
        ListPrint(L);//增加完成就进行更新文件
        return;
    }
    printf("查找失败，无法增加！\n");
}

void DeleteBookNumber(SqList L, BinTree T) {
    int s, i, n;
    BinTree p;
    printf("输入需要删除数量的书号：");
    scanf("%d", &s);
    i = BinarySearch(L, s);
    if (i) {
        printf("当前数量%d，请输入删除的数量：", L.elem[i].total);
        scanf("%d", &n);
        if (L.elem[i].total - n < 0) {
            printf("删除数量超过本来已有数量，删除失败！\n");
            return;
        }
        L.elem[i].total -= n;
        p = Find(T, L.elem[i]);
        p->Data.total -= n;
        printf("删除图书数量成功！\n");
        printf("当前数量%d\n", L.elem[i].total);
        ListPrint(L);//删除完成就进行更新文件
        return;
    }
    printf("查找失败，无法删除！\n");
}

void AddBook(SqList &L, BinTree T) {
    char name[30];
    printf("请输入需要增加的书名：");
    scanf("%s", &name);
    if (FindTitle(L, name) == 0) {
        printf("已经有这本书了\n");
        return;
    }
    book b;
    strcpy(b.name, name);
    int n;
    printf("请输入书号：");
    scanf("%d", &n);
    int f = BinarySearch(L, n);
    if (f == 0) {
        b.ISBN = n;
        printf("请依次输入数据(作者 价格 库存)：");
        scanf("%s %lf %d", &b.author, &b.price, &b.total);
        ListInsert_Sq(L, L.length, b);//直接在尾插入
        QSort(L, 1, L.length); //重新排序
        Insert(T, b);
        printf("增加成功！\n");
        ListPrint(L); //增加完成就进行更新文件
        return;
    }
    printf("书号错误，增加失败！\n");
}

void DeleteBook(SqList &L, BinTree &T) {
    // 删除图书
    char name[30];
    printf("请输入需要删除的书名：");
    scanf("%s", &name);
    int i = FindTitle(L, name);
    if (i) {
        book book;
        ListDelete_Sq(L, i, book);
        printf("%d",book.ISBN);
        Delete(T, book);
        printf("删除成功！\n");
        ListPrint(L); //删除完成就进行更新文件
        return;
    }
    printf("查找失败，无法删除！\n");
}

void ReviseBookPrice(SqList &L, BinTree &T) {
    // 修改图书价格
    int s, i, n;
    BinTree p;
    printf("输入需要修改价格的书号：");
    scanf("%d", &s);
    p = FindISBN(T, s);
    if (p) {
        printf("当前价格%.2f，请输入修改后的价格：", p->Data.price);
        scanf("%lf", &p->Data.price);
        i = BinarySearch(L,p->Data.ISBN);
        L.elem[i].price = p->Data.price;
        printf("修改价格成功！\n");
        ListPrint(L);//增加完成就进行更新文件
        return;
    }
    printf("查找失败，无法修改！\n");
}

void PriceRangeBook(SqList L) {
    int i;
    double m, n;
    printf("请输入价格范围：");
    scanf("%lf %lf", &m, &n);
    if (n < m) {
        printf("输入价格错误！");
        return;
    }
    int f=0;
    for (i = 1; i <= L.length; i++) {
        if (L.elem[i].price >= m && L.elem[i].price <= n) {
            printf("%d %s %s %.2lf %d\n", L.elem[i].ISBN, L.elem[i].name,
                   L.elem[i].author, L.elem[i].price, L.elem[i].total);
            f=1;
        }
    }
    if(f==0)
        printf("查找失败!\n");
}

void FindISBN(BinTree T) {
    int e;
    printf("输入需要查找的书号：");
    scanf("%d", &e);
    Position p = FindISBN(T,e);
    if (p)
        printf("%d %s %s %.2lf %d\n", p->Data.ISBN, p->Data.name,
               p->Data.author, p->Data.price, p->Data.total);
    else
        printf("查无此书！\n");
}

void FindTitle(SqList L) {
    char name[30];
    printf("输入需要查找的书名：");
    scanf("%s",&name);
    int i = FindTitle(L, name);
    if (i)
        printf("%d %s %s %.2lf %d\n", L.elem[i].ISBN, L.elem[i].name,
               L.elem[i].author, L.elem[i].price, L.elem[i].total);
    else
        printf("查无此书！\n");
}

void MainMenu() {
    printf("1、输出所有图书\n");
    printf("2、增加图书数量\n");
    printf("3、删除图书数量\n");
    printf("4、增加图书\n");
    printf("5、删除图书\n");
    printf("6、修改图书价格\n");
    printf("7、查询某价位范围的图书\n");
    printf("8、查找书库里是否有此书号\n");
    printf("9、查找书库里是否有此书名\n");
    printf("0、退出系统\n");
}

// 书号使用折半，书名使用顺序